package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.ExceptionTypeDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionExceptionDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;

import cn.edu.scau.cmi.hombio.domain.ExceptionType;
import cn.edu.scau.cmi.hombio.domain.Inspection;
import cn.edu.scau.cmi.hombio.domain.InspectionException;
import cn.edu.scau.cmi.hombio.domain.Pigsty;

import cn.edu.scau.cmi.hombio.service.InspectionExceptionService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for InspectionException entities
 * 
 */

@Controller("InspectionExceptionRestController")

public class InspectionExceptionRestController {

	/**
	 * DAO injected by Spring that manages ExceptionType entities
	 * 
	 */
	@Autowired
	private ExceptionTypeDAO exceptionTypeDAO;

	/**
	 * DAO injected by Spring that manages Inspection entities
	 * 
	 */
	@Autowired
	private InspectionDAO inspectionDAO;

	/**
	 * DAO injected by Spring that manages InspectionException entities
	 * 
	 */
	@Autowired
	private InspectionExceptionDAO inspectionExceptionDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for InspectionException entities
	 * 
	 */
	@Autowired
	private InspectionExceptionService inspectionExceptionService;

	/**
	 * Get Pigsty entity by InspectionException
	 * 
	 */
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/pigsty", method = RequestMethod.GET)
	@ResponseBody
	public Pigsty getInspectionExceptionPigsty(@PathVariable Integer inspectionexception_id) {
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception_id).getPigsty();
	}

	/**
	* Save an existing ExceptionType entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/exceptionType", method = RequestMethod.PUT)
	@ResponseBody
	public ExceptionType saveInspectionExceptionExceptionType(@PathVariable Integer inspectionexception_id, @RequestBody ExceptionType exceptiontype) {
		inspectionExceptionService.saveInspectionExceptionExceptionType(inspectionexception_id, exceptiontype);
		return exceptionTypeDAO.findExceptionTypeByPrimaryKey(exceptiontype.getId());
	}

	/**
	* View an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/pigsty/{pigsty_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pigsty loadInspectionExceptionPigsty(@PathVariable Integer inspectionexception_id, @PathVariable Integer related_pigsty_id) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty_id, -1, -1);

		return pigsty;
	}

	/**
	* Create a new ExceptionType entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/exceptionType", method = RequestMethod.POST)
	@ResponseBody
	public ExceptionType newInspectionExceptionExceptionType(@PathVariable Integer inspectionexception_id, @RequestBody ExceptionType exceptiontype) {
		inspectionExceptionService.saveInspectionExceptionExceptionType(inspectionexception_id, exceptiontype);
		return exceptionTypeDAO.findExceptionTypeByPrimaryKey(exceptiontype.getId());
	}

	/**
	* Create a new Pigsty entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/pigsty", method = RequestMethod.POST)
	@ResponseBody
	public Pigsty newInspectionExceptionPigsty(@PathVariable Integer inspectionexception_id, @RequestBody Pigsty pigsty) {
		inspectionExceptionService.saveInspectionExceptionPigsty(inspectionexception_id, pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* Select an existing InspectionException entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}", method = RequestMethod.GET)
	@ResponseBody
	public InspectionException loadInspectionException(@PathVariable Integer inspectionexception_id) {
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception_id);
	}

	/**
	* View an existing Inspection entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/inspection/{inspection_id}", method = RequestMethod.GET)
	@ResponseBody
	public Inspection loadInspectionExceptionInspection(@PathVariable Integer inspectionexception_id, @PathVariable Integer related_inspection_id) {
		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(related_inspection_id, -1, -1);

		return inspection;
	}

	/**
	* Delete an existing ExceptionType entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/exceptionType/{exceptiontype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteInspectionExceptionExceptionType(@PathVariable Integer inspectionexception_id, @PathVariable Integer related_exceptiontype_id) {
		inspectionExceptionService.deleteInspectionExceptionExceptionType(inspectionexception_id, related_exceptiontype_id);
	}

	/**
	* Create a new Inspection entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/inspection", method = RequestMethod.POST)
	@ResponseBody
	public Inspection newInspectionExceptionInspection(@PathVariable Integer inspectionexception_id, @RequestBody Inspection inspection) {
		inspectionExceptionService.saveInspectionExceptionInspection(inspectionexception_id, inspection);
		return inspectionDAO.findInspectionByPrimaryKey(inspection.getId());
	}

	/**
	* Save an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/pigsty", method = RequestMethod.PUT)
	@ResponseBody
	public Pigsty saveInspectionExceptionPigsty(@PathVariable Integer inspectionexception_id, @RequestBody Pigsty pigsty) {
		inspectionExceptionService.saveInspectionExceptionPigsty(inspectionexception_id, pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* Get ExceptionType entity by InspectionException
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/exceptionType", method = RequestMethod.GET)
	@ResponseBody
	public ExceptionType getInspectionExceptionExceptionType(@PathVariable Integer inspectionexception_id) {
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception_id).getExceptionType();
	}

	/**
	* Delete an existing Inspection entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/inspection/{inspection_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteInspectionExceptionInspection(@PathVariable Integer inspectionexception_id, @PathVariable Integer related_inspection_id) {
		inspectionExceptionService.deleteInspectionExceptionInspection(inspectionexception_id, related_inspection_id);
	}

	/**
	* Create a new InspectionException entity
	* 
	*/
	@RequestMapping(value = "/InspectionException", method = RequestMethod.POST)
	@ResponseBody
	public InspectionException newInspectionException(@RequestBody InspectionException inspectionexception) {
		inspectionExceptionService.saveInspectionException(inspectionexception);
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception.getId());
	}

	/**
	* Save an existing Inspection entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/inspection", method = RequestMethod.PUT)
	@ResponseBody
	public Inspection saveInspectionExceptionInspection(@PathVariable Integer inspectionexception_id, @RequestBody Inspection inspection) {
		inspectionExceptionService.saveInspectionExceptionInspection(inspectionexception_id, inspection);
		return inspectionDAO.findInspectionByPrimaryKey(inspection.getId());
	}

	/**
	* Show all InspectionException entities
	* 
	*/
	@RequestMapping(value = "/InspectionException", method = RequestMethod.GET)
	@ResponseBody
	public List<InspectionException> listInspectionExceptions() {
		return new java.util.ArrayList<InspectionException>(inspectionExceptionService.loadInspectionExceptions());
	}

	/**
	* Save an existing InspectionException entity
	* 
	*/
	@RequestMapping(value = "/InspectionException", method = RequestMethod.PUT)
	@ResponseBody
	public InspectionException saveInspectionException(@RequestBody InspectionException inspectionexception) {
		inspectionExceptionService.saveInspectionException(inspectionexception);
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception.getId());
	}

	/**
	* Delete an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/pigsty/{pigsty_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteInspectionExceptionPigsty(@PathVariable Integer inspectionexception_id, @PathVariable Integer related_pigsty_id) {
		inspectionExceptionService.deleteInspectionExceptionPigsty(inspectionexception_id, related_pigsty_id);
	}

	/**
	* Delete an existing InspectionException entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteInspectionException(@PathVariable Integer inspectionexception_id) {
		InspectionException inspectionexception = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception_id);
		inspectionExceptionService.deleteInspectionException(inspectionexception);
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* View an existing ExceptionType entity
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/exceptionType/{exceptiontype_id}", method = RequestMethod.GET)
	@ResponseBody
	public ExceptionType loadInspectionExceptionExceptionType(@PathVariable Integer inspectionexception_id, @PathVariable Integer related_exceptiontype_id) {
		ExceptionType exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(related_exceptiontype_id, -1, -1);

		return exceptiontype;
	}

	/**
	* Get Inspection entity by InspectionException
	* 
	*/
	@RequestMapping(value = "/InspectionException/{inspectionexception_id}/inspection", method = RequestMethod.GET)
	@ResponseBody
	public Inspection getInspectionExceptionInspection(@PathVariable Integer inspectionexception_id) {
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception_id).getInspection();
	}
}